import { defineStore } from "pinia";
import { ElMessage } from "element-plus";

class ForeignKeyStore {
  tableData: Array<any> = [];
}
export const useForeignKey = defineStore("foreignKey", {
  state() {
    return new ForeignKeyStore();
  },
  actions: {
    set(data: any) {
      if (data.id) {
        const dataId = this.tableData.findIndex((it) => it.id == data.id);
        if (dataId > -1) {
          this.tableData[dataId] = { ...data };
          ElMessage.success("修改成功");
        } else {
          ElMessage.warning("修改失败");
        }
      } else {
        this.tableData.push({
          ...data,
          id: new Date().getTime(),
        });
        ElMessage.success("添加成功");
      }
    },
    setData(data: any[]) {
      this.tableData = data;
    },
    delete(id: number | string) {
      const delIndex = this.tableData.findIndex((it) => it.id == id);
      if (delIndex > -1) {
        this.tableData.splice(delIndex, 1);
        ElMessage.success("删除成功");
      }
    },
    clear() {
      this.tableData = [];
    },
  },
});
